﻿/**
 * pointChartXML
 * Charts Collection
 *
 * @author		Duarte Peixinho, mail: duarte.peixinho@gmail.com
 * @version		1.0
 */

package com.duartepeixinho.charts.xml
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import XML;
	
	import com.duartepeixinho.charts.chartsType.PointsChart;

	public class PointChartXML extends Sprite 
	{
		private var _xmlLoader:URLLoader = new URLLoader();
		private var _xml:XML = new XML();
		
		//chart data
		private var _title:String;
		private var _width:Number;
		private var _height:Number;
		private var _startingValuesX:Number;
		private var _startingValuesY:Number;
		private var _countingNumbersX:Number;
		private var _countingNumbersY:Number;
		private var _intervalX:Number;
		private var _intervalY:Number;
		private var _labelsX:Boolean;
		private var _labelsY:Boolean;
		private var _labelsXaxis:String;
		private var _labelsYaxis:String;
		private var _grid:Boolean;
		private var _showZero:Boolean;
		
		//values
		private var _values:Array = new Array();
		
		/**
		 * Creates a Column Chart.
		 * 
		 * @param		xml			String		String of the location of the XML File
		 */
		
		public function PointChartXML(xml:String):void 
		{
			var req:URLRequest = new URLRequest(xml);
			req.data = 0;
			req.method = URLRequestMethod.POST;
			_xmlLoader.load(req);
			_xmlLoader.addEventListener(Event.COMPLETE, parseXML);
		}
		private function parseXML(e:Event):void {
			_xml = new XML(e.target.data);			
			getXMLData();
			
			//build chart
			var chart:PointsChart = new PointsChart(_values, new Point(_width, _height), new Point(_startingValuesX, _startingValuesY), new Point(_countingNumbersX, _countingNumbersY), _intervalX, _intervalY, _labelsX, _labelsY, _labelsXaxis, _labelsYaxis, _grid, _showZero, _title);
			addChild(chart);
		}
		
		private function getXMLData():void {
			_title = _xml.title;
			_width = _xml.properties.width;
			_height = _xml.properties.height;
			_startingValuesX = _xml.properties.startingValues.x;
			_startingValuesY = _xml.properties.startingValues.y;
			_countingNumbersX = _xml.properties.countingNumbers.x;
			_countingNumbersY = _xml.properties.countingNumbers.y;
			_intervalX = _xml.properties.interval.x;
			_intervalY = _xml.properties.interval.y;
			if (_xml.properties.labels.x == "true") _labelsX = true;
			else _labelsX = false;
			if (_xml.properties.labels.y == "true") _labelsY = true;
			else _labelsY = false;
			_labelsXaxis = _xml.properties.labels.xAxis;
			_labelsYaxis = _xml.properties.labels.yAxis;
			if (_xml.properties.grid == "true") _grid = true;
			else _grid = false;
			if (_xml.properties.showZero == "true") _showZero = true;
			else _showZero = false;
			
			//getting values
			for (var i:int = 0; i < _xml.points.point.length(); i++) {
				_values[i] = [Number(_xml.points.point[i].values.x), Number(_xml.points.point[i].values.y), Number(_xml.points.point[i].radius), String(_xml.points.point[i].color),_xml.points.point[i].label];				
			}
		}
	}
}